package ${initProjectVo.packageName}.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${initProjectVo.packageName}.dto.SysRoleMultiDTO;
import ${initProjectVo.packageName}.entity.Permission;
import ${initProjectVo.packageName}.entity.SysRole;
import ${initProjectVo.packageName}.entity.SysRolePermission;
import ${initProjectVo.packageName}.mapper.*;
import ${initProjectVo.packageName}.service.ISysRoleService;
import ${initProjectVo.packageName}.utils.TreeMenuUtil;
import ${initProjectVo.packageNameValue}.common.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author OFG
* @version 1.0
* @date 2021/1/6 20:09
* @Description
*/
@Service
public class ISysRoleServiceImpl implements ISysRoleService {
	@Autowired
	private SysRoleMapper sysRoleMapper;

	@Autowired
	private SysRolePermissionMapper sysRolePermissionMapper;
	@Autowired
	private PermissionMapper permissionMapper;

	@Transactional(readOnly = false,rollbackFor = Exception.class,propagation= Propagation.REQUIRED)
	@Override
	public void insertEntity(SysRole sysRole,Integer[] permissions) {
		sysRole.setCreateTime(new Date());
		sysRole.setAllowDelete(Constants.ZERO);
		sysRoleMapper.insert(sysRole);
		insertPermissionsAndNodes(sysRole, permissions);

	}

	@Transactional(readOnly = false,rollbackFor = Exception.class,propagation= Propagation.REQUIRED)
	@Override
	public void deletePrimaryKey(Integer id) {
		sysRoleMapper.deleteById(id);
		QueryWrapper<SysRolePermission> queryWrapper1 = new QueryWrapper<>();
		queryWrapper1.eq("role_id",id);
		sysRolePermissionMapper.delete(queryWrapper1);
	}

	@Transactional(readOnly = false,rollbackFor = Exception.class,propagation= Propagation.REQUIRED)
	@Override
	public void updateEntity(SysRole sysRole,Integer[] permissions) {
		sysRoleMapper.updateById(sysRole);
		QueryWrapper<SysRolePermission> queryWrapper1 = new QueryWrapper<>();
		queryWrapper1.eq("role_id",sysRole.getId());
		sysRolePermissionMapper.delete(queryWrapper1);
		insertPermissionsAndNodes(sysRole, permissions);
	}

	private void insertPermissionsAndNodes(SysRole sysRole, Integer[] permissions) {
		for (Integer perm : permissions) {
			SysRolePermission sysRolePermission = new SysRolePermission();
			sysRolePermission.setRoleId(sysRole.getId());
			sysRolePermission.setPermissionId(perm);
			sysRolePermissionMapper.insert(sysRolePermission);
		}
	}

	@Override
	public Map<String,Object> selectByPrimaryKey(Integer id) {
		QueryWrapper<SysRolePermission> queryWrapper1 = new QueryWrapper<>();
		queryWrapper1.select("permission_id").eq("role_id",id);
		List<Object> rolePermissionsList = sysRolePermissionMapper.selectObjs(queryWrapper1);

		Map<String,Object> dataMap = new HashMap<>();
		Integer[] rolePermissions = new Integer[rolePermissionsList.size()];

		dataMap.put("rolePermissions",rolePermissionsList.toArray(rolePermissions));

		dataMap.put("role",sysRoleMapper.selectById(id));
		return dataMap;
	}

	@Override
	public IPage<SysRoleMultiDTO> selectEntityList(Page<SysRoleMultiDTO> page,SysRoleMultiDTO record) {
		Map<String,Object> params = new HashMap<>();
		return sysRoleMapper.selectEntityList(page,params);
	}

	@Override
	public List<Object> getMenuList() {
		List<Permission> list = permissionMapper.selectList(null);
		TreeMenuUtil treeMenuUtil = new TreeMenuUtil();
		return treeMenuUtil.treeMenu(list);
	}

}
